package com.example.emb.controller;

import com.example.emb.entity.Stuworktable;
import com.example.emb.entity.pjo.MiniWork;
import com.example.emb.service.StudentService;
import com.example.emb.service.common.MinioService;
import com.example.emb.service.exception.NoRightToAccessException;
import com.example.emb.service.exception.UnableToWriteException;
import com.example.emb.service.exception.upLoadFailedException;
import com.example.emb.until.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jdk.dynalink.linker.LinkerServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@Api(tags = "学生的基本操作")
@RequestMapping("/zjgsEmb/Stu")

//主要就是写学生的器材上的操作
//第二个是写学生在作业上的操作
public class StuController {
    @Autowired
    MinioService minioService;
    @Autowired
    StudentService studentService;

    //查看教师发布的和自己有关联的作业
    @ApiOperation("查看教师发布的和自己有关联的作业")
    @GetMapping("/getAllMyWorks")
    public JsonResult<List<MiniWork>> getMyWorks(HttpSession session){
        int GroupId=Integer.parseInt(session.getAttribute("groupId").toString());
        Long Uid=Long.parseLong(session.getAttribute("uid").toString());
        List<MiniWork> list=studentService.GetWorkList(GroupId);
        return new JsonResult<List<MiniWork>>(200,list);
    }
    @ApiOperation("查看是不是已经有了的（判断是不是要新建作业）")
    @GetMapping("/GetIsExist")
    public JsonResult<Boolean> GetIsExist(HttpSession session,int workId){
        int GroupId=Integer.parseInt(session.getAttribute("groupId").toString());
        return new JsonResult<Boolean>(200,studentService.JudgeIsExist(GroupId,workId));
    }

    //新建作业（在教师发布的作业列表中）
    @ApiOperation("初始化/新建作业")
    @PostMapping("/InitHomeWork")
    public JsonResult<Stuworktable> InitWork(HttpSession session, int workId){
        int GroupId=Integer.parseInt(session.getAttribute("groupId").toString());
        Stuworktable stuworktable=studentService.InitGroupWork(GroupId,workId);
        return new JsonResult<Stuworktable>(200,stuworktable);
    }

    //查看作业内容，同时作为刷新的功能
    @ApiOperation("查看文本内容")
    @GetMapping("/readWorkDoc")
    public JsonResult<String> readWorkDoc(HttpSession session,int workId){
        int GroupId=Integer.parseInt(session.getAttribute("groupId").toString());
        Long Uid=Long.parseLong(session.getAttribute("uid").toString());
        try {
            String doc=studentService.getWorkDoc(Uid,GroupId,workId);
            return new JsonResult<String>(200,doc);
        }catch (NoRightToAccessException e){
            return new JsonResult<String>(601);
        }
    }

    //修改作业
    /**
     * 先在前端判断，再进行上传
     */
    @ApiOperation("修改作业")
    @GetMapping("/UpdateWork")
    public JsonResult<String> UpdateWork(HttpSession session,int workId,String NewDoc){
        int GroupId=Integer.parseInt(session.getAttribute("groupId").toString());
        Long Uid =Long.parseLong(session.getAttribute("uid").toString());
        try {
            Stuworktable stuworktable=studentService.UpdateGroupWork(Uid,GroupId,workId,NewDoc);
            return new JsonResult<String>(200,stuworktable.getWorkDoc());
        }catch (NoRightToAccessException e){
            return new JsonResult<String>(601);
        }catch (UnableToWriteException e2){
            return new JsonResult<String >(602);
        }

    }

    //上传和作业有关联的video
    //限制上传视频的大小
    @ApiOperation("上传作业视频")
    @PostMapping("/PostWorkVideo")
    public JsonResult<String> PosWorkVideo(MultipartFile file ,int WorkId){
        try {
            if (!file.getContentType().startsWith("video/")) {
                return new JsonResult<String>(604); // 返回错误代码，表示不是视频类型
            }
            if (file.getSize() > 350 * 1024 * 1024) {
                return new JsonResult<String>(605); // 返回错误代码，表示文件过大
            }
            studentService.upLoadFiles(file,WorkId,"Video");
            return new JsonResult<String>(200,"成功");
        }catch (upLoadFailedException e){
            return new JsonResult<String>(603);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JsonResult<String>(500);
    }

    //上传和作业有关联的zip
    //zip是全部的文件，限制大小500MB
    @ApiOperation("上传作业视频")
    @PostMapping("/PostWorkZip")
    public JsonResult<String> PosWorkZip(MultipartFile file ,HttpSession session,int WorkId){
        try {
            // 判断文件是否是zip类型
            String originalFilename = file.getOriginalFilename();
            if (originalFilename == null || !originalFilename.toLowerCase().endsWith(".zip")) {
                return new JsonResult<String>(606); // 返回错误代码，表示不是zip类型
            }

            // 判断文件大小是否小于500MB
            if (file.getSize() > 500 * 1024 * 1024) {
                return new JsonResult<String>(606); // 返回错误代码，表示文件过大
            }

            studentService.upLoadFiles(file, WorkId, "Zip");
            return new JsonResult<String>(200);
        } catch (upLoadFailedException e) {
            return new JsonResult<String>(603); // 返回上传失败的错误代码
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JsonResult<>(500);
    }

}
